package com.xmb.wechat.view.wechat.money;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nil.sdk.utils.DateUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.BankCardBean;
import com.xmb.wechat.bean.PayProofBean;
import com.xmb.wechat.common.Constant;
import com.xmb.wechat.definterface.SimpleTextWatcher;
import com.xmb.wechat.widget.TitleLayout;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/20 18:04
 */
public class WechatFundPayProofAddActivity extends BaseActivity {

    private static final int REQUEST_BANK_CARD = 717;

    @BindView(R2.id.title_layout)
    TitleLayout  mTitleLayout;
    @BindView(R2.id.tv_msg_time)
    TextView     mTvMsgTime;
    @BindView(R2.id.tv_start_time_title)
    TextView     mTvStartTimeTitle;
    @BindView(R2.id.tv_start_time)
    TextView     mTvStartTime;
    @BindView(R2.id.ll_start_time)
    LinearLayout mLlStartTime;
    @BindView(R2.id.tv_finish_time_title)
    TextView     mTvFinishTimeTitle;
    @BindView(R2.id.tv_finish_time)
    TextView     mTvFinishTime;
    @BindView(R2.id.ll_finish_time)
    LinearLayout mLlFinishTime;
    @BindView(R2.id.tv_bank_card)
    TextView     mTvBankCard;
    @BindView(R2.id.ll_bank_card)
    LinearLayout mLlBankCard;
    @BindView(R2.id.et_1)
    EditText     mEt1;
    @BindView(R2.id.et_2)
    EditText     mEt2;
    @BindView(R2.id.et_3)
    EditText     mEt3;

    private int          mType;
    private int          count      = 0;
    private PayProofBean mPayProofBean;
    private String[]     finishTime = new String[]{"两小时内", "当日", "次日"};

    public WechatFundPayProofAddActivity() {
        super(R.layout.activity_fund_pay_proof_add);
    }

    public static void start(Activity activity, int type, int requestCode) {
        Intent intent = new Intent(activity, WechatFundPayProofAddActivity.class);
        intent.putExtra("type", type);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void initView() {
        mPayProofBean = new PayProofBean();
        mType = getIntent().getIntExtra("type", 0);
        mPayProofBean.setType(mType);
        switch (mType) {
            case Constant.PayProofType.TYPE_WITHDRAW_START:
                mPayProofBean.setTypeName("零钱提现发起");
                mTitleLayout.setTitle("零钱提现发起");
                mLlStartTime.setVisibility(View.VISIBLE);
                mTvStartTimeTitle.setText("发起时间");
                mLlFinishTime.setVisibility(View.VISIBLE);
                mTvFinishTimeTitle.setText("预计到账时间");
                mLlBankCard.setVisibility(View.VISIBLE);
                mEt1.setVisibility(View.VISIBLE);
                mEt1.setHint("提现金额");
                break;
            case Constant.PayProofType.TYPE_WITHDRAW_FINISH:
                mPayProofBean.setTypeName("零钱提现到账");
                mTitleLayout.setTitle("零钱提现到账");
                mLlStartTime.setVisibility(View.VISIBLE);
                mTvStartTimeTitle.setText("发起时间");
                mLlFinishTime.setVisibility(View.VISIBLE);
                mTvFinishTimeTitle.setText("到账时间");
                mLlBankCard.setVisibility(View.VISIBLE);
                mEt1.setVisibility(View.VISIBLE);
                mEt1.setHint("提现金额");
                break;
            case Constant.PayProofType.TYPE_QRCODE_FINISH_NOTIFICATION:
                mPayProofBean.setTypeName("二维码收款到账");
                mTitleLayout.setTitle("二维码收款到账");
                mLlStartTime.setVisibility(View.VISIBLE);
                mTvStartTimeTitle.setText("支付时间");
                mEt1.setVisibility(View.VISIBLE);
                mEt1.setHint("本次收款金额");
                mEt2.setVisibility(View.VISIBLE);
                mEt2.setHint("今日累计笔数");
                mEt3.setVisibility(View.VISIBLE);
                mEt3.setHint("今日累计金额");
                break;
            case Constant.PayProofType.TYPE_PAY_PROOF_GR:
                mPayProofBean.setTypeName("支付凭证(个人)");
                mTitleLayout.setTitle("支付凭证(个人)");
                mLlStartTime.setVisibility(View.VISIBLE);
                mTvStartTimeTitle.setText("支付时间");
                mEt1.setVisibility(View.VISIBLE);
                mEt1.setHint("填写金额");
                mEt2.setVisibility(View.VISIBLE);
                mEt2.setHint("填写收款人姓名");
                mEt2.setInputType(InputType.TYPE_CLASS_TEXT);
                mEt3.setVisibility(View.VISIBLE);
                mEt3.setHint("填写支付方式");
                mEt3.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case Constant.PayProofType.TYPE_PAY_PROOF_SH:
                mPayProofBean.setTypeName("支付凭证(商户)");
                mTitleLayout.setTitle("支付凭证(商户)");
                mLlStartTime.setVisibility(View.VISIBLE);
                mTvStartTimeTitle.setText("支付时间");
                mEt1.setVisibility(View.VISIBLE);
                mEt1.setHint("填写金额");
                mEt2.setVisibility(View.VISIBLE);
                mEt2.setHint("填写商家名称");
                mEt2.setInputType(InputType.TYPE_CLASS_TEXT);
                mEt3.setVisibility(View.VISIBLE);
                mEt3.setHint("填写支付方式");
                mEt3.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
        }
        mTitleLayout.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayProofBean payProofBean = checkValueAvaliable();
                if (payProofBean == null)
                    return;
                setResult(RESULT_OK, new Intent());
                WechatStartApplication.getBoxStore().boxFor(PayProofBean.class).put(payProofBean);
                WechatFundPayProofAddActivity.this.finish();
            }
        });
        mEt1.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                mPayProofBean.setMoney(value);
            }
        });
        mEt2.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                if (mType == Constant.PayProofType.TYPE_QRCODE_FINISH_NOTIFICATION)
                    mPayProofBean.setCountNumber(value);
                else if (mType == Constant.PayProofType.TYPE_PAY_PROOF_GR)
                    mPayProofBean.setReceiveName(value);
                else if (mType == Constant.PayProofType.TYPE_PAY_PROOF_SH)
                    mPayProofBean.setReceiveName(value);
            }
        });
        mEt3.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChangedFinish(String value) {
                if (mType == Constant.PayProofType.TYPE_QRCODE_FINISH_NOTIFICATION)
                    mPayProofBean.setCountMoney(value);
                else if (mType == Constant.PayProofType.TYPE_PAY_PROOF_GR || mType == Constant.PayProofType.TYPE_PAY_PROOF_SH)
                    mPayProofBean.setPayType(value);
            }
        });
    }


    @OnClick({R2.id.ll_msg_time, R2.id.ll_start_time, R2.id.ll_finish_time, R2.id.ll_bank_card})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.ll_msg_time) {
            KeyboardUtils.hideSoftInput(this);
            showTimeChoose((date, v) -> {
                mPayProofBean.setMsgTime(date.getTime());
                mTvMsgTime.setText(DateUtils.getStringByFormat(date, "yyyy-MM-dd HH:mm:ss"));
            });
        } else if (id == R.id.ll_start_time) {
            KeyboardUtils.hideSoftInput(this);
            showTimeChoose((date, v) -> {
                mPayProofBean.setStartTime(DateUtils.getStringByFormat(date, "yyyy-MM-dd HH:mm:ss"));
                mTvStartTime.setText(DateUtils.getStringByFormat(date, "yyyy-MM-dd HH:mm:ss"));
            });
        } else if (id == R.id.ll_finish_time) {
            KeyboardUtils.hideSoftInput(this);
            if (mType == Constant.PayProofType.TYPE_WITHDRAW_START) {
                mTvFinishTime.setText(finishTime[count]);
                mPayProofBean.setFinishTime(finishTime[count]);
                count++;
                if (count == 3)
                    count = 0;
            } else {
                showTimeChoose((date, v) -> {
                    mPayProofBean.setFinishTime(DateUtils.getStringByFormat(date, "yyyy-MM-dd HH:mm:ss"));
                    mTvFinishTime.setText(DateUtils.getStringByFormat(date, "yyyy-MM-dd HH:mm:ss"));
                });
            }
        } else if (id == R.id.ll_bank_card) {
            WechatFundBankCardActivity.startforResult(this, REQUEST_BANK_CARD);
        }
    }

    private void showTimeChoose(OnTimeSelectListener listener) {
        @SuppressLint("SimpleDateFormat")
        TimePickerView pvTime = new TimePickerBuilder(this, listener).setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setTitleSize(20)//标题文字大小
                .setTitleText("时间")//标题文字
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setSubmitColor(getResources().getColor(R.color.wechat_tab_bg_green))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.wechat_tab_bg_green))//取消按钮文字颜色
                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();
        pvTime.show();
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_BANK_CARD) {
            if (data == null)
                return;
            BankCardBean result = (BankCardBean) data.getSerializableExtra("result");
            String strBankCard = result.getBankName() + "(" + result.getBankNumber() + ")";
            mTvBankCard.setText(strBankCard);
            mPayProofBean.setBankCard(strBankCard);
        }
    }

    private PayProofBean checkValueAvaliable() {
        switch (mType) {
            case Constant.PayProofType.TYPE_WITHDRAW_START:
                if (mPayProofBean.getMsgTime() == 0) {
                    ToastUtils.showShort("请选择消息时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getStartTime())) {
                    ToastUtils.showShort("请选择发起时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getFinishTime())) {
                    ToastUtils.showShort("请选择预计到账时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getBankCard())) {
                    ToastUtils.showShort("请选择银行卡");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getMoney())) {
                    ToastUtils.showShort("请填写提现金额");
                    return null;
                }
                //修改预计到账时间
                mPayProofBean.setFinishTime(getRealExpectedTime(count));
                break;
            case Constant.PayProofType.TYPE_WITHDRAW_FINISH:
                if (mPayProofBean.getMsgTime() == 0) {
                    ToastUtils.showShort("请选择消息时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getStartTime())) {
                    ToastUtils.showShort("请选择发起时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getFinishTime())) {
                    ToastUtils.showShort("请选择到账时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getBankCard())) {
                    ToastUtils.showShort("请选择银行卡");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getMoney())) {
                    ToastUtils.showShort("请填写提现金额");
                    return null;
                }
                break;
            case Constant.PayProofType.TYPE_QRCODE_FINISH_NOTIFICATION:
                if (mPayProofBean.getMsgTime() == 0) {
                    ToastUtils.showShort("请选择消息时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getStartTime())) {
                    ToastUtils.showShort("请选择支付时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getMoney())) {
                    ToastUtils.showShort("请填写收款金额");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getCountNumber())) {
                    ToastUtils.showShort("请填写累计笔数");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getCountMoney())) {
                    ToastUtils.showShort("请填写累计金额");
                    return null;
                }
                break;
            case Constant.PayProofType.TYPE_PAY_PROOF_GR:
                if (mPayProofBean.getMsgTime() == 0) {
                    ToastUtils.showShort("请选择消息时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getStartTime())) {
                    ToastUtils.showShort("请选择支付时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getMoney())) {
                    ToastUtils.showShort("请填写金额");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getReceiveName())) {
                    ToastUtils.showShort("请填写收款人姓名");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getPayType())) {
                    ToastUtils.showShort("请填写支付方式");
                    return null;
                }
                break;
            case Constant.PayProofType.TYPE_PAY_PROOF_SH:
                if (mPayProofBean.getMsgTime() == 0) {
                    ToastUtils.showShort("请选择消息时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getStartTime())) {
                    ToastUtils.showShort("请选择支付时间");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getMoney())) {
                    ToastUtils.showShort("请填写金额");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getReceiveName())) {
                    ToastUtils.showShort("请填写商家名称");
                    return null;
                }
                if (TextUtils.isEmpty(mPayProofBean.getPayType())) {
                    ToastUtils.showShort("请填写支付方式");
                    return null;
                }
                break;
        }
        return mPayProofBean;
    }

    @SuppressLint("SimpleDateFormat")
    private String getRealExpectedTime(int count) {
        String startTime = mPayProofBean.getStartTime();
        if (TextUtils.isEmpty(startTime))
            return "";
        switch (count) {
            case 0://2小时后
                try {
                    Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
                    long time = parse.getTime();
                    time += 2 * 60 * 60 * 1000;
                    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                break;
            case 1://当日
                try {
                    Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
                    return new SimpleDateFormat("yyyy-MM-dd ").format(parse) + "24:00:00";
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                break;
            case 2://次日
                try {
                    Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
                    long time = parse.getTime();
                    time += 24 * 60 * 60 * 1000;
                    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                break;
        }
        return "";
    }

}
